package data;

import java.util.Calendar;
import java.util.List;

import core.Log;
import core.Utility;

/*	Name
 *  Start
 *  Duration
 *  Priority
 *  Labels
 *  List
 *  End
 *  Description
 *  Repetition
 *  Ticked
 */

public class Event
{
	private String name;
	private EventDateTime start;
	private int duration; //duration in hours?
	private int priority;
	private List<String> labels;
	private String list;
	private String id;
	
	private EventDateTime end;
	private String description;
	private int repetition;
	private boolean ticked;
	private boolean starred;
	private boolean deadlineEvent;
	
	public Event() {
		Log.debug("Event: Entering default event constructor" );
		name = "";
		start = new EventDateTime();
		duration = 0;
		priority = -1;
		labels = null;
		list = null;
		end = null;
		description = null;
		//repetition = -1;
		ticked = false;
		id = null;
		starred = false;
		deadlineEvent = false;
	}

	public Event(String name) {
		
		this();
		Log.debug("Event: Entering event constructor with name as param" );
		this.name = name;
	}

	public Event(String eventName, EventDateTime startDateTime,
			EventDateTime endDateTime, int eventDuration, int stringToPriority, List<String> labels2,
			String eventList) {
		this();
		Log.debug("Event: Entering event constructor with full param list" );
		name = eventName;
		start = startDateTime;
		end = endDateTime;
		duration = eventDuration; 

		if (!endDateTime.isDefaultTime()) {
			assert(start.compareTo(end) <= 0);
			duration = Utility.millisToMins(end.getTimeInMilli() - start.getTimeInMilli());
		}
		if (duration > 0) {
			int durationMin = duration;
			end = new EventDateTime(start.getTimeInMilli());
			if (duration >= 60) {
				end.add(Calendar.HOUR, duration/60);
				durationMin = duration % 60;
			}
			end.add(Calendar.MINUTE, durationMin);
		}
		priority = stringToPriority; //TODO Implement enum
		labels = labels2;
		list = eventList;
	}
	
	public int getPriority() {
		Log.debug("Event: Entering getPriority" );
		return priority;
	}
	
	public void setPriority(int priority) {
		Log.debug("Event: Entering setPriority" );
		this.priority = priority;
	}
	
	public List<String> getLabels() {
		Log.debug("Event: Entering getLabels" );
		return labels;
	}
	
	public void setLabels(List<String> labels) {
		Log.debug("Event: Entering setLabels" );
		this.labels = labels;
	}
	
	public String getList() {
		Log.debug("Event: Entering getList" );
		return list;
	}
	
	public void setList(String list) {
		Log.debug("Event: Entering setList" );
		this.list = list;
	}
	

	public String getName() {
		Log.debug("Event: Entering getName" );
		return name;
	}

	public void setName(String name) {
		Log.debug("Event: Entering setName" );
		this.name = name;
	}

	public String getDescription() {
		Log.debug("Event: Entering getDescription" );
		return description;
	}

	public void setDescription(String desc) {
		Log.debug("Event: Entering setDescription" );
		this.description = desc;
	}

	public EventDateTime getStart() {
		Log.debug("Event: Entering getStart" );
		return start;
	}
	
	public void setStart(EventDateTime start) {
		Log.debug("Event: Entering setStart" );
		this.start = start;
	}

	public EventDateTime getEnd() {
		Log.debug("Event: Entering getEnd" );
		return end;
	}

	public void setEnd(EventDateTime end) {
		Log.debug("Event: Entering setEnd" );
		this.end = end;
	}

	public int getDuration() {
		Log.debug("Event: Entering getDuration" );
		return duration;
	}

	public void setDuration(int duration) {
		Log.debug("Event: Entering setDuration" );
		this.duration = duration;
		if (duration > 0) {
			end = new EventDateTime(start.getTimeInMilli());
			end.add(Calendar.MINUTE, duration);
		}
	}
	
	public boolean getTicked() {
		Log.debug("Event: Entering getTicked" );
		return this.ticked;
	}
	
	public void setTicked(boolean value) {
		Log.debug("Event: Entering setTicked" );
		this.ticked = value;
	}
	
	public boolean toggleTicked() {
		Log.debug("Event: Entering toggleTicked" );
		if (ticked) {
			Log.warn("Event: Object was unticked by toggleTick function");
			ticked = false;
		} else {
			Log.warn("Event: Object was ticked by the toggleTick function");
			ticked = true;
		}
		
		return ticked;
	}
	public boolean getStarred() {
		Log.debug("Event: Entering getStarred" );
		return starred;
	}
	
	public void setStarred(boolean value) {
		Log.debug("Event: Entering setStarred" );
		starred = value;
	}

	public boolean toggleStarred() {
		Log.debug("Event: Entering toggleStarred" );
		if (starred) {
			Log.warn("Event: Object was unstarred by the toggleStarred function");
			starred = false;
		} else {
			Log.warn("Event: Object was starred by the toggleStarred function");
			starred = true;
		}
		return starred;
	}
	
	public String toString() {
		Log.debug("Event: Entering toString" );
		String eventToString =null;
		
		if(name != null)
			eventToString = name;
		
		if(start != null)
			eventToString += " " + start.toString();

		if(duration != 0)
			eventToString += " " + duration;

		if(priority != -1)
			eventToString += " " + priority;
		
		if (labels != null) {
			for (String label : labels) {
				eventToString += " label:" + label;
			}
		}
		
		if(list == null)
			eventToString += " list:" + "default";
		else {
			eventToString += " list:" + list;
		}
		
		if (end != null) {
			eventToString += " end:" + end.toString();
		}
		if(description != null)
			eventToString += " desc:" + description;

		if(repetition != 0)
			eventToString += " rep:" + repetition;
		Log.warn("Null fields are NOT added to event string");
		return eventToString;
		
	}
	
	public boolean equals(Object to) {
		//FIXME this function needs to be checked
		Log.debug("Event: Entering equals" );
		boolean isEqual = false;
		String thisObjString = null;
		String compareToObjString = null;
		if (to == null) {
			return false;
		}
		if (!(to instanceof Event)) {
			return false;
		}
		Event compareTo = (Event)to; 

		//FIXME I think it is not right to have this?
//		if (this.name == null || compareTo.name == null) {	
//			return false;
//		}
		thisObjString = this.toString();
		compareToObjString = compareTo.toString();
		return thisObjString.equals(compareToObjString);
	}
	
	public String getId() {
		Log.debug("Event: Entering getId" );
		return id;
	}
	
	public void setId(String id) {
		Log.debug("Event: Entering setId" );
		this.id = id;
	}
	
	public boolean getDeadlineEvent() {
		Log.debug("Event: Entering getDeadlineEvent" );
		return deadlineEvent;
	}
	
	public void setDeadlineEvent(boolean newDeadline) {
		Log.debug("Event: Entering setDeadlineEvent" );
		this.deadlineEvent = newDeadline;
	}
	
}
